package com.example.doudizhu;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.function.Consumer;

public class PokerGame3 {//new该对象表示开始一局游戏

    static ArrayList<String> list = new ArrayList<>();
    static HashMap<String, Integer> hm = new HashMap<>();


    //生成牌堆
    static {
        String[] color = {"♠", "♥", "♣", "♦"};
        String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        for (String col : color) {
            for (String n : num) {
                String s = col + n;
                list.add(s);
            }
        }
        list.add(" 小王");
        list.add(" 大王");
        hm.put("J", 18);
        hm.put("Q", 19);
        hm.put("K", 20);
        hm.put("A", 21);
        hm.put("2", 22);
        hm.put("小王", 23);
        hm.put("大王", 24);

    }


    public PokerGame3() {
        //洗牌
        Collections.shuffle(list);
        //发牌
        ArrayList<String> lord = new ArrayList<>();
        ArrayList<String> player1 = new ArrayList<>();
        ArrayList<String> player2 = new ArrayList<>();
        ArrayList<String> player3 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String temp = list.get(i);
            if (i <= 2) lord.add(temp);
            else {
                if (i % 3 == 0) player1.add(temp);
                else if (i % 3 == 1) {
                    player2.add(temp);
                } else {
                    player3.add(temp);
                }
            }
        }
        //排序并且看牌
        lookpoker("底牌", lord);
        lookpoker("玩家1", player1);
        lookpoker("玩家2", player2);
        lookpoker("玩家3", player3);

    }

    Integer getValue(String poker) {
        String key = poker.substring(1);
        if (hm.containsKey(key)) return hm.get(key);
        else {
            return Integer.parseInt(key);
        }
    }

    void lookpoker(String name, ArrayList<String> pokers) {
        Collections.sort(pokers, (o1, o2) -> {
                    int i = getValue(o1) - getValue(o2);
                    return i == 0 ? o1.substring(0, 1).compareTo(o2.substring(0, 1)) : i;
                }
        );
        System.out.print(name + " :");
        pokers.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.print(s + " ");
            }
        });
        System.out.println();
    }

}
